package com.fingard.dsp.bank.directbank.cmbc01;

import java.sql.Connection;
import java.util.*;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillBankQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB001OperBillQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB005BillBackQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB006BillStateQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB013BilStateQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.ReqB012BillTrialAmtQry;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.RetAtsB012BillTrialAmtQry;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB012BillTrialAmtQryDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * Created by F on 2018/12/17.
 */
@Component("CMBC01ElecBill")
public class CMBC01ElecBill extends CMBC01Base implements IElecBillBankQuery {
    //REQSEQID=票据编号，BANKREQSEQID=票据id
    private static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,BANKREQSEQID) values (?,?,?,?,?);";
    private static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";

    /**
     * 可操作票据查询
     */
    @Override
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws  Exception{
        RetAtsB001OperBillQry retAts =  null;
        while (query.readRecord()){
            //提示承兑
            if("1".equals(query.detail.queryType) && "2".equals(query.detail.busiType)){//查询待提示承兑的票据服务ok
                retAts=b001OperBillQryAll(query,"QryAcceptbleDraft");
            }else if("2".equals(query.detail.queryType) && "2".equals(query.detail.busiType)){//查询提示承兑代签收票据×
                retAts=b001OperBillQryAll(query,"QryAcceptSignUpDraft");
            }else if("3".equals(query.detail.queryType) && "2".equals(query.detail.busiType)){//查询可执行撤销提示承兑服务ok
                retAts=b001OperBillQryAll(query,"QryCancelAcceptDraft");
                //撤票
            }else if("3".equals(query.detail.queryType) && "1".equals(query.detail.busiType)){//查询可撤票票据信息列表ok
                retAts=b001OperBillQryAll(query,"QryDestructableDraft");
                //提示付款
            }else if("1".equals(query.detail.queryType) && "12".equals(query.detail.busiType)){//提示付款票据列表查询ok
                retAts=b001OperBillQryAll(query,"QryIssueableDraft");
            }else if("2".equals(query.detail.queryType) && "12".equals(query.detail.busiType)){//查询提示付款代签收列表×
                retAts=b001OperBillQryAll(query,"QryStaySignUpDraft");
            }else if("3".equals(query.detail.queryType) && "12".equals(query.detail.busiType)){//查询可提示付款撤回票据ok
                retAts=b001OperBillQryAll(query,"QryCancelIssueDraft");
                //提示收票
            }else if("1".equals(query.detail.queryType) && "3".equals(query.detail.busiType)){//查询可提示收票服务的票据ok
                retAts=b001OperBillQryAll(query,"QryPresentableDraft");
            }else if("2".equals(query.detail.queryType) && "3".equals(query.detail.busiType)){//查询提示收票待签收票据ok
                retAts=b001OperBillQryAll(query,"QryIssuanceSignUpDraft");
            }else if("3".equals(query.detail.queryType) && "3".equals(query.detail.busiType)){//查询可撤销提示收票的票据ok
                retAts=b001OperBillQryAll(query,"QryCancelableIssuanceDraft");
                //背书
            }else if("1".equals(query.detail.queryType) && "4".equals(query.detail.busiType)){//查询可背书的票据ok
                retAts=b001OperBillQryAll(query,"QryEndorseableDraft");
            }else if("2".equals(query.detail.queryType) && "4".equals(query.detail.busiType)){//查询可被背书签收票据ok
                retAts=b001OperBillQryAll(query,"QrySignUpEndorsmtDraft");
            }else if("3".equals(query.detail.queryType) && "4".equals(query.detail.busiType)){//查询可背书撤回票据ok
                retAts=b001OperBillQryAll(query,"QryCancelableEndrsmtDraft");
                //贴现
            }else if("1".equals(query.detail.queryType) && "10".equals(query.detail.busiType)){//查询可贴现的票据
                retAts=b001OperBillQryAll(query,"QryDiscountableBill");
            }else if("3".equals(query.detail.queryType) && "10".equals(query.detail.busiType)){//查询可贴现撤回票据
                retAts=b001OperBillQryAll(query,"QryCancelableDiscountableBill");
                //出票
            }else if("1".equals(query.detail.queryType) && "1".equals(query.detail.busiType)){
                retAts=b001OperQryIsBillApply(query,"QryStayRegDraft");
            }else {
                retAts=new RetAtsB001OperBillQry(query.reqHeader);
                retAts.respCode="-1";
                retAts.respInfo="暂不支持其它查询操作";
                retAts.writeBefore();
                retAts.writeEnd();
                return retAts;
            }
        }
        return retAts;
    }

    /**
     * 票据操作明细查询
     */
    @Override
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws  Exception{
        return null;
    }

    /**
     * 正面信息查询 ok
     */
    @Override
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query) throws  Exception{
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo ="";
        Connection conn= FGBiz.getLocalDbConn();
        try{
            while (query.readRecord()) {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "QryDraftInfo";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                String billId="";
                if(selMap.size()>0){
                    billId=selMap.get("BANKREQSEQID").toString();//票据id
                }else {
                    retAts = new RetAtsB004BillFrontQry(query.reqHeader);
                    retAts.writeStart();
                    retAts.respCode ="-1";
                    retAts.respInfo = "票据号与id映射异常";
                    retAts.writeRetDetail();
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据号
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if ("0".equals(tmpRetCode)) {
                            while (tmpBkReader.read("/CMBC/xDataBody")) {
                                retAts.respCode = "0";
                                retAts.respInfo = "";
                                retAts.billNo = tmpBkReader.readedXElement.getSingleTextNotNull("billNo");//电子票据
                                retAts.billType = tmpBkReader.readedXElement.getSingleTextNotNull("billType");//票据类型 1-银承 2-商承
                                retAts.issDate = tmpBkReader.readedXElement.getSingleTextNotNull("issueDt");//出票日
                                retAts.dueDate = tmpBkReader.readedXElement.getSingleTextNotNull("dueDt");//到期日
                                retAts.billAmt = tmpBkReader.readedXElement.getSingleTextNotNull("billMoney");//电子票据金额
                                if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    retAts.transFlag = "0";//背书转让标识 1-可再转让 0-不得转让
                                }else if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    retAts.transFlag = "1";//背书转让标识 1-可再转让 0-不得转让
                                }
                                retAts.mustPay = "";//承兑无条件支付委托 1-无条件支付 0-非条件支付
                                retAts.drawName = tmpBkReader.readedXElement.getSingleTextNotNull("remitter");//出票人
                                retAts.drawCode = "";//出票人组织代码
                                retAts.drawAct = tmpBkReader.readedXElement.getSingleTextNotNull("remitterAcct");//出票人账号
                                retAts.drawBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("remitterBank");//出票人开户行行名
                                retAts.drawBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("remitterBankNo");//出票人开户行行号
                                retAts.creditLv = tmpBkReader.readedXElement.getSingleTextNotNull("remitterCreditClass");//出票人信用等级
                                retAts.gradeAgent = tmpBkReader.readedXElement.getSingleTextNotNull("remitterCreditAgency");//出票人信用评级机构
                                retAts.gradeDueDate = tmpBkReader.readedXElement.getSingleTextNotNull("remitterCreditDueDt");//出票人评级到期日
                                retAts.accpName = tmpBkReader.readedXElement.getSingleTextNotNull("acceptor");//承兑人名称
                                retAts.accpAct = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorAcct");//承兑人账号
                                if(StringHelper.isNullOrEmpty(retAts.accpAct)){
                                    retAts.accpAct="0";
                                }
                                retAts.accpBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBank");//承兑人开户行行名
                                retAts.accpBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBankNo");//承兑人开户行行号
                                retAts.accpCredit = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorCreditClass");//承兑人信用等级
                                retAts.accpGrade = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorCreditAgency");//承兑人评级机构
                                retAts.accpGradeDue = tmpBkReader.readedXElement.getSingleTextNotNull("acceptorCreditDueDt");//承兑人评级到期日
                                List<XElement> guarnteeList=tmpBkReader.readedXElement.getSingleChild("guarnteeInfo").getChildList("Map");
                                if(guarnteeList!=null && guarnteeList.size()>0){
                                    for(XElement guarnteeMap:guarnteeList){
                                        String assuType=guarnteeMap.getSingleTextNotNull("assuType");
                                        if("1".equals(assuType)){//出票保证
                                            retAts.drGuarName = guarnteeMap.getSingleTextNotNull("guartrName");//出票保证人名称
                                            retAts.drGuarAddr = guarnteeMap.getSingleTextNotNull("guarntrAddr");//出票保证人地址
                                            retAts.drGuarDate = guarnteeMap.getSingleTextNotNull("warteeDt");//出票保证日期
                                        }else if("2".equals(assuType)){//承兑保证
                                            retAts.apGuarName = guarnteeMap.getSingleTextNotNull("guartrName");//承兑保证人名称
                                            retAts.apGuarAddr = guarnteeMap.getSingleTextNotNull("guarntrAddr");//承兑保证人地址
                                            retAts.apGuarDate = guarnteeMap.getSingleTextNotNull("warteeDt");//承兑保证日期
                                        }
                                    }
                                }
                                retAts.payeeName = tmpBkReader.readedXElement.getSingleTextNotNull("payee");//收款人名称
                                retAts.payeeAct = tmpBkReader.readedXElement.getSingleTextNotNull("payeeAcct");//收款人账号
                                retAts.payeeBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("payeeBank");//收款人开户行行名
                                retAts.payeeBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("payeeBankNo");//收款人开户行行号
                                retAts.billState = tmpBkReader.readedXElement.getSingleTextNotNull("curStatusName");//票据状态
                                retAts.accpDate = tmpBkReader.readedXElement.getSingleTextNotNull("acceptDt");//承兑日
                                retAts.contractNo = tmpBkReader.readedXElement.getSingleTextNotNull("conferNo");//合同文本编号
                                retAts.invoiceNo = tmpBkReader.readedXElement.getSingleTextNotNull("invoiceNo");//发票号
                                retAts.billBeforeOwner = tmpBkReader.readedXElement.getSingleTextNotNull("billBeforeOwner");//交易前手
                            }
                        } else {
                            respCode = "-1";
                            respInfo = tmpRetMessage;
                        }
                    } else {
                        respCode = "-1";
                        respInfo = tmpBkReader.toString();
                    }
                } else {
                    respCode = "-1";
                    respInfo =tmpStrRet[1];
                }
            }
        }catch (Exception e){
            respCode = "-1";
            respInfo ="查询票据正面信息异常："+e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询票据正面信息异常："+e.getMessage());
        }finally {
            conn.close();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB004BillFrontQry(query.reqHeader);
            retAts.writeStart();
            retAts.respCode =respCode;
            retAts.respInfo = respInfo;
        }
        retAts.writeRetDetail();
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据背面信息查询   ok
     */
    @Override
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception{
        RetAtsB005BillBackQry retAts = new RetAtsB005BillBackQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo ="";
        Connection conn= FGBiz.getLocalDbConn();
        try{
            while (query.readRecord()) {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "QryDraftInfo";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                String billId="";
                if(selMap.size()>0){
                    billId=selMap.get("BANKREQSEQID").toString();//票据id
                }else {
                    retAts = new RetAtsB005BillBackQry(query.reqHeader);
                    retAts.writeStart();
                    retAts.writeRespCode("-1");
                    retAts.writeRespCode("票据号与id映射异常");
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据id
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if ("0".equals(tmpRetCode)) {
                            while (tmpBkReader.read("/CMBC/xDataBody/backInfo/Map")){
                                String endoType=tmpBkReader.readedXElement.getSingleTextNotNull("endoType");//背书类型
                                if(StringHelper.hasAnyChar(endoType)){
                                    RetAtsB005BillBackQryDetail detail = new RetAtsB005BillBackQryDetail();
                                    detail.respCode="0";
                                    detail.respInfo="";
                                    //电子票据号码
                                    detail.billNo = query.detail.billNo;
                                    //操作代码取值
                                    /// 交易类型代码 2  承兑 3  收票 4  撤票 10  背书转让 11  贴现 12  回购式贴现赎回 13  转贴现 14  回购式转贴现赎回 15  再贴现 16  回购式再贴现赎回 25  央行卖出商业汇票 17  保证 18  质押 19  质押解除 20  提示付款 21  逾期提示付款 22  追索 23  追索同意清偿
                                    detail.transCode = getTransCode(endoType);
                                    //申请人
                                    detail.applyName = tmpBkReader.readedXElement.getSingleTextNotNull("fromName");
                                    //接收人
                                    detail.receName = tmpBkReader.readedXElement.getSingleTextNotNull("toName");
                                    //申请日期
                                    detail.applyDate = tmpBkReader.readedXElement.getSingleTextNotNull("endoDate");
                                    //签收日期
                                    detail.respDate = tmpBkReader.readedXElement.getSingleTextNotNull("signDate");
                                    if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("protEndors"))){
                                        detail.transFlag = "0";//背书转让标识 1-可再转让 0-不得转让
                                    }else if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("protEndors"))){
                                        detail.transFlag = "1";//背书转让标识 1-可再转让 0-不得转让
                                    }
                                    detail.guarAddr=tmpBkReader.readedXElement.getSingleTextNotNull("assuAdrr");//保证人地址
                                    detail.repOpenDate=tmpBkReader.readedXElement.getSingleTextNotNull("redeemOpenDate");//回购开放日
                                    detail.repDueDate=tmpBkReader.readedXElement.getSingleTextNotNull("redeemEndDate");//回购截止日期
                                    String respFlag=tmpBkReader.readedXElement.getSingleTextNotNull("payFlag");
                                    if(StringHelper.hasAnyChar(respFlag)){
                                        detail.respFlag=Integer.parseInt(respFlag)+1+"";//应答标记 1--签收 2--驳回
                                    }
                                    detail.unPayCode="";//拒付理由代码 1--DC00 2--DC01 N--DC0(N-1) 99--DC09
                                    detail.unPayMemo=tmpBkReader.readedXElement.getSingleTextNotNull("payRefuReson");//拒付理由
                                    String recourseType=tmpBkReader.readedXElement.getSingleTextNotNull("recType");
                                    if(StringHelper.hasAnyChar(recourseType)){
                                        detail.recourseType=Integer.parseInt(recourseType)+1+"";//1-拒付追索 2-非拒付追索
                                    }
                                    retAts.writeDetail(detail);
                                }
                            }
                        } else {
                            respCode = "-1";
                            respInfo = tmpRetMessage;
                        }
                    } else {
                        respCode = "-1";
                        respInfo = tmpBkReader.toString();
                    }
                } else {
                    respCode = "-1";
                    respInfo =tmpStrRet[1];
                }
            }
        }catch (Exception e){
            respCode = "-1";
            respInfo ="查询票据背面信息异常："+e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询票据背面信息异常："+e.getMessage());
        }finally {
            conn.close();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB005BillBackQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(respCode);
            retAts.writeRespCode(respInfo);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据交易状态查询  ok
     */
    @Override
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query)throws  Exception{
        RetAtsB006BillStateQry retAts  = new RetAtsB006BillStateQry(query.reqHeader);
        RetAtsB006BillStateQryDetail detail=new RetAtsB006BillStateQryDetail();
        retAts.writeStart();
        Connection conn=FGBiz.getLocalDbConn();
        try{
            //赋值通用参数
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "qryDraftStatus";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            while (query.readRecord()) {
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                //1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
                xmlWriter.writeElementString("tranType",getTranType(query.detail.busiType,query.detail.operType));
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if(tmpStrRet[0].length() == 0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if(tmpRetCode.equals("0")){
                            while(tmpBkReader.read("/CMBC/xDataBody/List/Map")){
                                detail.respCode = "0";//响应码
                                detail.respInfo = tmpBkReader.readedXElement.getSingleTextNotNull("processResult");//响应信息
                                detail.reqSeqID= query.detail.reqSeqID;//交易流水号
                                detail.billNo = tmpBkReader.readedXElement.getSingleTextNotNull("billNo");//电子票据号码
                                detail.transState = tmpBkReader.readedXElement.getSingleTextNotNull("operStatus");//处理状态码
                                detail.payInfoCode = detail.transState;//交易信息码

                                String billId=tmpBkReader.readedXElement.getSingleTextNotNull("rgctId");//电子票据id
                                if(StringHelper.hasAnyChar(detail.billNo)){
                                    //根据票据号查询票据id，存在则不映射
                                    Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{detail.billNo, query.reqHeader.bankCode});
                                    if (selMap.size() == 0) {
                                        //票据id和票据号映射，便于操作类接口使用
                                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                        JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, query.reqHeader.bankCode, Format.Date8Format.format(new Date()), detail.billNo, billId});
                                    }
                                }else {//银行不放billNo，说明只到新增票据信息
                                    detail.billNo = billId;
                                }

                                if(!"false".equals(getBankConfig().getSetValue("isNeedBillStatuFlag","","B006"))){
                                    //ATSES-29063 民生银行电票，查询票据状态时选取账号不对
                                    String remitterAcct=query.detail.actNum;//当前操作账号
//                                    String remitterAcct=tmpBkReader.readedXElement.getSingleTextNotNull("remitterAcct");//出票人账号
                                    detail.billState = getBillStatus(billId,remitterAcct);//票据状态
                                }
                            }
                        } else{
                            detail.respCode = "-1";
                            detail.respInfo = tmpRetMessage;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            WriteBankLogLn("票据交易状态查询异常："+ex);
        }finally {
            conn.close();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 可追索人查询 未提供接口
     */
    @Override
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception{
        return null;
    }

    /**
     * 历史票据查询 未提供接口
     */
    @Override
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception{
        return null;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }

    /**
     * 贴现金额试算
     */
    @Override
    public RetAtsB012BillTrialAmtQry D_B012_TrialAmtQuery(ReqB012BillTrialAmtQry query) throws Exception{
        RetAtsB012BillTrialAmtQry retAts=new RetAtsB012BillTrialAmtQry(query.reqHeader);
        RetAtsB012BillTrialAmtQryDetail detail=new RetAtsB012BillTrialAmtQryDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()) {
            try {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "DisAmountCalculate";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("discRate", query.detail.discRate);//贴现利率
                xmlWriter.writeElementString("disDate", query.detail.discDate);//贴现日期
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if("0".equals(tmpRetCode)){
                            detail.respCode = "0";
                            detail.respInfo = tmpRetMessage;
                            detail.reqSeqID = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/insId");
                            detail.interest = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/interest");
                            detail.disAmount = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/disAmount");
                            detail.billMoney = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/billMoney");
                        }else {
                            detail.respCode = "-1";
                            detail.respInfo = tmpRetMessage;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("贴现异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据交易类查询
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsB013BillStateQry D_B013_BillStateQuery(ReqB013BillStateQry query) throws Exception {
        RetAtsB013BillStateQry retAts = new RetAtsB013BillStateQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        try{
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            Map<String,Integer> tmpTotalNum = new HashMap<String, Integer>();
            while (tmpNowReadNum==0 || tmpNowReadNum<tmpTotalNum.get("totalCount")) {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "QryDraftBusinessTrans";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("businessType", getTranType(query.detail.busiType,query.detail.operType));//业务类型
                xmlWriter.writeElementString("beginDate", query.detail.startDate);//开始日期
                xmlWriter.writeElementString("endDate", query.detail.endDate);//结束日期
                xmlWriter.writeElementString("currentIndex", tmpNowReadStart+"");//查询的当前页
                xmlWriter.writeElementString("pageSize", getBankConfig().getValueAsString("pageSize"));//每页显示条数
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if(tmpRetCode.equals("0")){
                            String totalCount = tmpBkReader.getFirstText("/CMBC/xDataBody/allNum");//获取总记录数
                            String indexCount = tmpBkReader.getFirstText("/CMBC/xDataBody/totalNum");//当前页的数量
                            if(tmpTotalNum.containsKey("totalCount")){
                                if(tmpTotalNum.get("totalCount").intValue()!=Integer.valueOf(totalCount).intValue()){
                                    retAts=new RetAtsB013BillStateQry(query.reqHeader);
                                    retAts.respCode="-1";
                                    retAts.respInfo = "相同条件前后查出来的总数不一致";
                                    retAts.writeBefore();
                                    retAts.writeEnd();
                                    return retAts;
                                }
                            }else{
                                if("0".equals(totalCount)){
                                    retAts.respCode ="0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                }
                                tmpTotalNum.put("totalCount",Integer.valueOf(totalCount));//记录总条数
                            }
                            int readCount=0;
                            while (tmpBkReader.read("/CMBC/xDataBody/List/Map")){
                                RetAtsB013BilStateQryDetail detail=new RetAtsB013BilStateQryDetail();
                                //电子票据号码
                                detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("billNo");
                                //交易合同号码
                                detail.contractNo=tmpBkReader.readedXElement.getSingleTextNotNull("conferNo");
                                //发票号码
                                detail.invoiceNo=tmpBkReader.readedXElement.getSingleTextNotNull("invoiceNo");
                                //票据类型 1-银承 2-商承
                                detail.billType=tmpBkReader.readedXElement.getSingleTextNotNull("billType");
                                //票据种类 1-实物 2-电子
                                detail.billClass=tmpBkReader.readedXElement.getSingleTextNotNull("billClass");
                                //出票日(yyyy-MM-dd)
                                detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("issueDt"));
                                //到期日(yyyy-MM-dd)
                                detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("dueDt"));
                                //承兑日（yyyy-MM-dd）
                                detail.accpDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("acceptDt"));
                                //电子票据金额
                                detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("billMoney");
                                //出票人
                                detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("remitter");
                                //出票人组织代码
                                detail.drawCode="";
                                //出票人账号
                                detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("remitterAcct");
                                //出票人开户行行名
                                detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBank");
                                //出票人开户行行号
                                detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBankNo");

                                //承兑人名称
                                detail.accpName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptor");
                                detail.accpCode="";
                                //承兑人账号
                                detail.accpAct=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorAcct");
                                //承兑人开户行行名
                                detail.accpBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBank");
                                //承兑人开户行行号
                                detail.accpBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBankNo");

                                //收款人
                                detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("payee");
                                detail.payeeCode="";
                                //收款人账号
                                detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("payeeAcct");
                                //收款人开户行行名
                                detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBank");
                                //收款人开户行行号
                                detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBankNo");

                                //背书转让标识 1-可再转让 0-不得转让
                                if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="0";
                                }else if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="1";
                                }else{
                                    WriteBankLogLn("票据号"+detail.billNo+"返回的背书转让标识无法识别");
                                    continue;
                                }
                                //清算标记 1-线上清算 2-线下清算
                                if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("settlementMark"))){
                                    detail.payType="2";
                                }else if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("settlementMark"))){
                                    detail.payType="1";
                                }else{
                                    WriteBankLogLn("票据号"+detail.billNo+"返回的背书转让标识无法识别");
                                    continue;
                                }
                                //申请人
                                detail.applyName=tmpBkReader.readedXElement.getSingleTextNotNull("applicantName");
                                //申请人组织机构代码
                                detail.applyCode=tmpBkReader.readedXElement.getSingleTextNotNull("applicantOrgCode");
                                //申请人账号
                                detail.applyAct=tmpBkReader.readedXElement.getSingleTextNotNull("applicantAcctNo");
                                //申请人开户行行名
                                detail.applyBnkName="";
                                //申请人开户行行号
                                detail.applyBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("applicantBankNo");
                                //接收人
                                detail.receName=tmpBkReader.readedXElement.getSingleTextNotNull("receiverName");
                                //接收人组织机构代码
                                detail.receCode="";
                                //接收人账号
                                detail.receAct=tmpBkReader.readedXElement.getSingleTextNotNull("receiverAcctNo");
                                //接收人开户行行名
                                detail.receBnkName="";
                                //接收人开户行行号
                                detail.receBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("receiverBankNo");
                                //备注
                                detail.memo=tmpBkReader.readedXElement.getSingleTextNotNull("memo");
                                //操作状态
                                detail.transState=tmpBkReader.readedXElement.getSingleTextNotNull("processResult");
                                retAts.writeDetail(detail);
                                readCount++;
                            }
                            //查询记录数和遍历数不一致
                            if(readCount!=Integer.valueOf(indexCount)){
                                respCode="-1";
                                respInfo="统计明细有误";
                                break;
                            }
                            tmpNowReadStart++;//页数+1
                            tmpNowReadNum +=readCount;
                        } else if("E4014".equals(tmpRetCode)){
                            respCode="0";
                            respInfo="无记录";
                            break;
                        }else{
                            respCode="-1";
                            respInfo=tmpRetMessage;
                            break;
                        }
                    }else {
                        respCode="-1";
                        respInfo=tmpSbMsg.toString();
                        break;
                    }
                }else {
                    respCode="-1";
                    respInfo=tmpStrRet[1];
                    break;
                }
            }
        }catch (Exception e){
            respCode="-1";
            respInfo=e.getMessage();
            e.printStackTrace();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB013BillStateQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }


    /**
     * 通用查询接口  ok
     * @param query 请求参数
     * @param trnCode 请求类型
     * @return
     * @throws Exception
     */
    private RetAtsB001OperBillQry b001OperBillQryAll(ReqB001OperBillQry query,String trnCode) throws Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        Connection conn= FGBiz.getLocalDbConn();
        try{
            int tmpNowReadStart = 1;//起始页
            int tmpNowReadNum = 0;//已查询笔数
            Map<String,Integer> tmpTotalNum = new HashMap<String, Integer>();
            while (tmpNowReadNum==0 || tmpNowReadNum<tmpTotalNum.get("totalCount")) {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = trnCode;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("pageSize", getBankConfig().getValueAsString("pageSize"));//每页显示条数
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("currentIndex", tmpNowReadStart+"");//查询的当前页
                if("QryDiscountableBill".equals(trnCode) || "QryCancelableDiscountableBill".equals(trnCode)){
                    xmlWriter.writeElementString("billClass", "2");//票据种类： 1－实物； 2－电子
                    xmlWriter.writeElementString("billType", query.detail.billType);//票据类型 空－全部； 1－银票； 2－商票
                    xmlWriter.writeElementString("maxBillMoney", query.detail.billAmtUp);//票据金额上限
                    xmlWriter.writeElementString("minBillMoney", query.detail.billAmtDown);//票据金额下限
                }
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if(tmpRetCode.equals("0")){
                            String totalCount = tmpBkReader.getFirstText("/CMBC/xDataBody/allNum");//获取总记录数
                            String indexCount = tmpBkReader.getFirstText("/CMBC/xDataBody/totalNum");//当前页的数量
                            if(tmpTotalNum.containsKey("totalCount")){
                                if(tmpTotalNum.get("totalCount").intValue()!=Integer.valueOf(totalCount).intValue()){
                                    retAts=new RetAtsB001OperBillQry(query.reqHeader);
                                    retAts.respCode="-1";
                                    retAts.respInfo = "相同条件前后查出来的总数不一致";
                                    retAts.writeBefore();
                                    retAts.writeEnd();
                                    return retAts;
                                }
                            }else{
                                if("0".equals(totalCount)){
                                    retAts.respCode ="0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                }
                                tmpTotalNum.put("totalCount",Integer.valueOf(totalCount));//记录总条数
                            }
                            int readCount=0;
                            while (tmpBkReader.read("/CMBC/xDataBody/List/Map")){
                                RetAtsB001OperBillQryDetail detail=new RetAtsB001OperBillQryDetail();
                                if("QryAcceptbleDraft".equals(trnCode) || "QryAcceptSignUpDraft".equals(trnCode) || "QryCancelAcceptDraft".equals(trnCode)){//提示承兑申请，方式默认为1
                                    detail.mustPay="1";
                                }
                                String billType = tmpBkReader.readedXElement.getSingleTextNotNull("billType");
                                if (!billType.equals(query.detail.billType)) {
                                    readCount++;
                                    continue;
                                }
                                //票据类型 1-银承 2-商承
                                detail.billType = billType;
                                //出票日(yyyy-MM-dd)
                                detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("issueDt"));
                                //到期日(yyyy-MM-dd)
                                detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("dueDt"));
                                //电子票据金额
                                detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("billMoney");
                                //背书转让标识 1-可再转让 0-不得转让
                                if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="0";
                                }else if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="1";
                                }else{
                                    WriteBankLogLn("票据号"+detail.billNo+"返回的背书转让标识无法识别");
                                    continue;
                                }
                                //出票人
                                detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("remitter");
                                //出票人组织代码
                                detail.drawCode="";
                                //出票人账号
                                detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("remitterAcct");
                                //出票人开户行行名
                                detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBank");
                                //出票人开户行行号
                                detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBankNo");
                                //承兑人名称
                                detail.accpName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptor");
                                detail.accpCode="";
                                //承兑人账号
                                detail.accpAct=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorAcct");
                                //承兑人开户行行名
                                detail.accpBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBank");
                                //承兑人开户行行号
                                detail.accpBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBankNo");
                                detail.accpDate=tmpBkReader.readedXElement.getSingleTextNotNull("acceptDt");
                                //收款人
                                detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("payee");
                                detail.payeeCode="";
                                //收款人账号
                                detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("payeeAcct");
                                //收款人开户行行名
                                detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBank");
                                //收款人开户行行号
                                detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBankNo");
                                //交易明细流水
                                detail.busiNo="";
                                if(!"1".equals(query.detail.queryType)){//非申请类型参数
                                    //申请日期
                                    detail.applyDate=tmpBkReader.readedXElement.getSingleTextNotNull("applyDate");
                                    //申请人
                                    detail.applyName=tmpBkReader.readedXElement.getSingleTextNotNull("applicantName");
                                    //申请人组织机构代码
                                    detail.applyCode=tmpBkReader.readedXElement.getSingleTextNotNull("applicantOrgCode");
                                    //申请人账号
                                    detail.applyAct=tmpBkReader.readedXElement.getSingleTextNotNull("applicantAcctNo");
                                    //申请人开户行行名
                                    detail.applyBnkName="";
                                    //申请人开户行行号
                                    detail.applyBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("applicantBankNo");
                                    //接收人
                                    detail.receName=tmpBkReader.readedXElement.getSingleTextNotNull("receiverName");
                                    //接收人组织机构代码
                                    detail.receCode="";
                                    //接收人账号
                                    detail.receAct=tmpBkReader.readedXElement.getSingleTextNotNull("receiverAcctNo");
                                    //接收人开户行行名
                                    detail.receBnkName="";
                                    //接收人开户行行号
                                    detail.receBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("receiverBankNo");
                                    if("12".equals(query.detail.busiType)){//提示付款类型参数
                                        //清算标记 1-线上清算 2-线下清算
                                        detail.payType="0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("settlementMark"))?"2"
                                                :tmpBkReader.readedXElement.getSingleTextNotNull("settlementMark");
                                        //逾期原因说明
                                        detail.overDueRea=tmpBkReader.readedXElement.getSingleTextNotNull("overDueReason");
                                        //备注
                                        detail.memo=tmpBkReader.readedXElement.getSingleTextNotNull("remark");
                                    }
                                    if("10".equals(query.detail.busiType)){//贴现类型参数
                                        String discType=tmpBkReader.readedXElement.getSingleTextNotNull("discType");
                                        if("RM00".equals(discType)){
                                            discType="1";
                                        }else if("RM01".equals(discType)){
                                            discType="2";
                                        }else {
                                            WriteBankLogLn("票据号"+detail.billNo+"返回的贴现标识无法识别");
                                            continue;
                                        }
                                        detail.discType=discType;
                                        detail.payType=getBankConfig().getMapKeyByValue("payTypeMapping",tmpBkReader.readedXElement.getSingleTextNotNull("settlementMark"));//清算标记 1-线上清算 2-线下清算
                                        detail.repOpenDate=tmpBkReader.readedXElement.getSingleTextNotNull("rpdOpenDt");//回购开放日期
                                        detail.repDueDate=tmpBkReader.readedXElement.getSingleTextNotNull("rpdDueDt");//回购截止日期
                                        detail.discRate=tmpBkReader.readedXElement.getSingleTextNotNull("discRate");//贴现利率
                                        detail.repRate=tmpBkReader.readedXElement.getSingleTextNotNull("rpdDiscRate");//回购利率
                                        detail.actedAct=tmpBkReader.readedXElement.getSingleTextNotNull("inAcctNo");//入账账号
                                        detail.actedBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("inBankNo");//入账行号
                                    }
                                }
                                //交易合同号码
                                detail.contractNo=tmpBkReader.readedXElement.getSingleTextNotNull("conferNo");
                                //发票号码
                                detail.invoiceNo=tmpBkReader.readedXElement.getSingleTextNotNull("invoiceNo");
                                //票据id
                                String billId=tmpBkReader.readedXElement.getSingleTextNotNull("rgctId");
                                //电子票据号码
                                detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("billNo");
                                if(StringHelper.hasAnyChar(detail.billNo)){
                                    //根据票据号查询票据id，存在则不映射
                                    Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{detail.billNo, query.reqHeader.bankCode});
                                    if (selMap.size() == 0) {
                                        //票据id和票据号映射，便于操作类接口使用
                                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                        JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, query.reqHeader.bankCode, Format.Date8Format.format(new Date()), detail.billNo, billId});
                                    }
                                }else{//银行不放billNo，说明只到新增票据信息
                                    detail.billNo = billId;
                                }
                                //票据状态
                                if(!"false".equals(getBankConfig().getSetValue("isNeedBillStatuFlag","","B001"))){
                                    detail.billStatus=getBillStatus(billId,query.detail.actNum);
                                }
                                retAts.writeDetail(detail);
                                readCount++;
                            }
                            //查询记录数和遍历数不一致
                            if(readCount!=Integer.valueOf(indexCount)){
                                respCode="-1";
                                respInfo="统计明细有误";
                                break;
                            }
                            tmpNowReadStart++;//页数+1
                            tmpNowReadNum +=readCount;
                        } else if("E4014".equals(tmpRetCode)){
                            respCode="0";
                            respInfo="无记录";
                            break;
                        }else{
                            respCode="-1";
                            respInfo=tmpRetMessage;
                            break;
                        }
                    }else {
                        respCode="-1";
                        respInfo=tmpSbMsg.toString();
                        break;
                    }
                }else {
                    respCode="-1";
                    respInfo=tmpStrRet[1];
                    break;
                }
            }
        }catch (Exception e){
            respCode="-1";
            respInfo=e.getMessage();
            e.printStackTrace();
            WriteBankLogLn(trnCode+"查询可操作票据异常");
        }finally {
            if(conn!=null){
                conn.close();
            }
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 可登记票据信息查询接口
     * @param query 请求参数
     * @param trnCode 请求类型
     * @return
     * @throws Exception
     */
    private RetAtsB001OperBillQry b001OperQryIsBillApply(ReqB001OperBillQry query,String trnCode) throws Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        Connection conn= FGBiz.getLocalDbConn();
        try{
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            Map<String,Integer> tmpTotalNum = new HashMap<String, Integer>();
            while (tmpNowReadNum==0 || tmpNowReadNum<tmpTotalNum.get("totalCount")) {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = trnCode;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("pageSize", getBankConfig().getValueAsString("pageSize"));//每页显示条数
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("currentIndex", tmpNowReadStart+"");//查询的当前页
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if(tmpRetCode.equals("0")){
                            String totalCount = tmpBkReader.getFirstText("/CMBC/xDataBody/draft/allNum");//获取总记录数
                            String indexCount = tmpBkReader.getFirstText("/CMBC/xDataBody/draft/totalNum");//当前页的数量
                            if(tmpTotalNum.containsKey("totalCount")){
                                if(tmpTotalNum.get("totalCount").intValue()!=Integer.valueOf(totalCount).intValue()){
                                    retAts=new RetAtsB001OperBillQry(query.reqHeader);
                                    retAts.respCode="-1";
                                    retAts.respInfo = "相同条件前后查出来的总数不一致";
                                    retAts.writeBefore();
                                    retAts.writeEnd();
                                    return retAts;
                                }
                            }else{
                                if("0".equals(totalCount)){
                                    retAts.respCode ="0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                }
                                tmpTotalNum.put("totalCount",Integer.valueOf(totalCount));//记录总条数
                            }
                            int readCount=0;
                            while (tmpBkReader.read("/CMBC/xDataBody/draft/List/Map")){
                                RetAtsB001OperBillQryDetail detail=new RetAtsB001OperBillQryDetail();
                                //票据类型 1-银承 2-商承
                                detail.billType=tmpBkReader.readedXElement.getSingleTextNotNull("billType");
                                //出票日(yyyy-MM-dd)
                                detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("issueDt"));
                                //到期日(yyyy-MM-dd)
                                detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("dueDt"));
                                //电子票据金额
                                detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("billMoney");
                                //背书转让标识 1-可再转让 0-不得转让
                                if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="0";
                                }else if("0".equals(tmpBkReader.readedXElement.getSingleTextNotNull("banEndorsementMark"))){
                                    detail.transFlag="1";
                                }else{
                                    WriteBankLogLn("票据号"+detail.billNo+"返回的背书转让标识无法识别");
                                    continue;
                                }
                                //出票人
                                detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("remitter");
                                //出票人账号
                                detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("remitterAcct");
                                //出票人开户行行名
                                detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBank");
                                //出票人开户行行号
                                detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("remitterBankNo");
                                //承兑人名称
                                detail.accpName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptor");
                                //承兑人账号
                                detail.accpAct=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorAcct");
                                //承兑人开户行行名
                                detail.accpBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBank");
                                //承兑人开户行行号
                                detail.accpBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("acceptorBankNo");
                                //收款人
                                detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("payee");
                                //收款人账号
                                detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("payeeAcct");
                                //收款人开户行行名
                                detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBank");
                                //收款人开户行行号
                                detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("payeeBankNo");
                                //交易合同号码
                                detail.contractNo=tmpBkReader.readedXElement.getSingleTextNotNull("conferNo");
                                //发票号码
                                detail.invoiceNo=tmpBkReader.readedXElement.getSingleTextNotNull("invoiceNo");
                                //票据id
                                String billId=tmpBkReader.readedXElement.getSingleTextNotNull("rgctId");
                                //电子票据号码
                                detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("billNo");
                                if(StringHelper.hasAnyChar(detail.billNo)){
                                    //根据票据号查询票据id，存在则不映射
                                    Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{detail.billNo, query.reqHeader.bankCode});
                                    if (selMap.size() == 0) {
                                        //票据id和票据号映射，便于操作类接口使用
                                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                        JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, query.reqHeader.bankCode, Format.Date8Format.format(new Date()), detail.billNo, billId});
                                    }
                                }else{//银行不放billNo，说明只到新增票据信息
                                    detail.billNo = billId;
                                }
                                //票据状态
                                if(!"false".equals(getBankConfig().getSetValue("isNeedBillStatuFlag","","B001"))){
                                    detail.billStatus=getBillStatus(detail.billNo,query.detail.actNum);
                                }
                                retAts.writeDetail(detail);
                                readCount++;
                            }
                            //查询记录数和遍历数不一致
                            if(readCount!=Integer.valueOf(indexCount)){
                                respCode="-1";
                                respInfo="统计明细有误";
                                break;
                            }
                            tmpNowReadStart++;//页数+1
                            tmpNowReadNum +=readCount;
                        } else if("E4014".equals(tmpRetCode)){
                            respCode="0";
                            respInfo="无记录";
                            break;
                        }else{
                            respCode="-1";
                            respInfo=tmpRetMessage;
                            break;
                        }
                    }else {
                        respCode="-1";
                        respInfo=tmpSbMsg.toString();
                        break;
                    }
                }else {
                    respCode="-1";
                    respInfo=tmpStrRet[1];
                    break;
                }
            }
        }catch (Exception e){
            respCode="-1";
            respInfo=e.getMessage();
            e.printStackTrace();
            WriteBankLogLn(trnCode+"查询可操作票据异常");
        }finally {
            if(conn!=null){
                conn.close();
            }
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }
}